{% from 'include/input_macros.html' import input, checkbox %}
{% if not adding %}
<table class="overview" id="ajax-users">
	<thead>
		<tr class="overviewHead">
			<th class="padding10 first-collumn" id="user-login-th">{{lang.words.login|title()}}</th>
			<th style="width: 10%">{{lang.words.password|title()}}</th>
			<th style="width: 10%" class="checkbox-head" id="user-active-th">{{lang.words.active|title()}}</th>
			<th style="width: 20%">{{lang.words.email|title()}}</th>
			{% if g.user_params['role'] == 2 %}
			<th style="width: 10%" id="user-role-th">{{lang.words.role|title()}}</th>
			{% else %}
				<th style="width: 10%" id="user-group-role-th">{{lang.words.groups|title()}} {{lang.words.and}} {{lang.words.roles|title()}}</th>
			{% endif %}
			<th style="width: 100%" id="user-services-th">{{lang.words.services|title()}}</th>
			<th></th>
			<th></th>
		</tr>
	</thead>
	<tbody>
	{% endif %}
	{% for user in users %}
	    {% set disable_superAdmin = {'disabled': ''} %}
        {% if g.user_params['role'] == 2 %}
            {% for user_role in users_roles %}
                {% if user_role.user_role_id == 1 and user.user_id == user_role.user_id %}
                    {% if disable_superAdmin.update({'disabled': 'disabled'}) %} {% endif %}
                {% endif %}
            {% endfor %}
        {% endif %}
		<tr id="user-{{user.user_id}}" class="{{ loop.cycle('odd', 'even') }} {% if adding %}newuser{% endif %}">
			<td class="padding10 first-collumn">
				{% set login_id = 'login-' + user.user_id|string() %}
				{% if user.ldap_user == 1 %}
					{{ input(login_id, value=user.username, readonly='readonly', disabled=disable_superAdmin['disabled']) }}
				{% else %}
					{{ input(login_id, value=user.username, disabled=disable_superAdmin['disabled']) }}
				{% endif %}
			</td>
			<td>
				{% if user.ldap_user != 1 and not disable_superAdmin['disabled'] %}
				<span title="{{lang.words.change|title()}} {{lang.words.password}}" style="cursor: pointer; margin-left: 15px;" class="div-pic" onclick="openChangeUserPasswordDialog('{{user.user_id}}')">
				</span>
				{% endif %}
			</td>
			<td class="checkbox">
				{% set activeuser_id = 'activeuser-' + user.user_id|string() %}
				{% if user.activeuser == 1 %}
					{{ checkbox(activeuser_id, checked='checked', disabled=disable_superAdmin['disabled']) }}
				{% else %}
					{{ checkbox(activeuser_id, disabled=disable_superAdmin['disabled']) }}
				{% endif %}
			</td>
			<td>
				{% set email_id = 'email-' + user.user_id|string() %}
				{% if user.ldap_user == 1%}
					{{ input(email_id, value=user.email, readonly='readonly', disabled=disable_superAdmin['disabled']) }}
				{% else %}
					{{ input(email_id, value=user.email, type='email', disabled=disable_superAdmin['disabled']) }}
				{% endif %}
			</td>
			<td>
                {% if g.user_params['role'] == 2 %}
				<select id="role-{{user.user_id}}" name="role-{{user.user_id}}" {{disable_superAdmin['disabled']}}>
					<option disabled selected>Select a role</option>
					{% for r in roles %}
						{% for user_role in user_roles %}
							{% if r.role_id|int() != 1 and user.user_id|string() == user_role.user_id|string() %}
								{% if user_role.user_role_id|string() == r.role_id|string() %}
								<option value="{{ r.role_id }}" selected>{{ r.name }}</option>
								{% else %}
								<option value="{{ r.role_id }}">{{ r.name }}</option>
								{% endif %}
							{% endif %}
						{% endfor %}
					{% endfor %}
				</select>
				{% else %}
				<span title="{{lang.words.change2|title()}} {{lang.words.user3}} {{lang.words.groups}} {{lang.words.and}} {{lang.words.roles}}" style="cursor: pointer; margin-left: 40px;" class="div-pic" onclick="confirmChangeGroupsAndRoles('{{user.user_id}}')"></span>
				{% endif %}
			</td>
			<td>
				<span title="{{lang.words.change2|title()}} {{lang.words.user3}} {{lang.words.services}}" style="cursor: pointer; margin-left: 15px;" class="div-pic" onclick="openChangeUserServiceDialog('{{user.user_id}}')"></span>
			</td>
			<td>
				<a href="/app/history/user/{{user.user_id}}" title="{{lang.words.view|title()}} {{lang.words.history}} {{lang.words.for}} {{lang.words.user2}}" class="history"></a>
			</td>
			<td>
				<a class="delete" onclick="confirmDeleteUser({{user.user_id}})" title="{{lang.words.delete|title()}} {{user.username}}" style="cursor: pointer;"></a>
			</td>
		</tr>
	{% endfor %}
	{% if not adding %}
	<tbody>
</table>
<br /><span class="add-button" title="{{lang.words.add|title()}} {{lang.words.new}} {{lang.words.user2}}" id="add-user-button">+ {{lang.words.add|title()}}</span>
<br /><br />
{% endif %}
